﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class InputNewsConfig : IWebPartConfig
    {
        public string Id
        {
            get
            {
                return "InputNewsConfig";
            }
        }
        public string Subject
        {
            get
            {
                return ResourceBLL.GetText(Id, "Title", "Cấu hình nhập tin bài");
            }
        }

        void GetDataForTreeView(ref int LevelIndex,ref DataTable dtTree, string GroupNewsId, string ParentGroupNewsItemId)
        {
            DataTable dtTemp = new GroupNewsBLL().ReadingGroupNewsItems(GroupNewsId, ParentGroupNewsItemId);
            //DataTable dtTemp = new GroupNewsBLL().ReadingGroupNewsItems(GroupNewsId, ParentGroupNewsItemId);
            for (int iIndex = 0; iIndex < dtTemp.Rows.Count; iIndex++)
            {
                string GroupNewsItemId = (string)dtTemp.Rows[iIndex]["GroupNewsItemId"];
                string GroupNewsItemName = "(N/A)";
                try
                {
                    GroupNewsItemName = (string)dtTemp.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                }
                catch { }
                for (int iLevelIndex = 1; iLevelIndex <= LevelIndex; iLevelIndex++)
                {
                    GroupNewsItemName = "-" + GroupNewsItemName;
                }

                dtTree.Rows.Add(new object[]{
                    GroupNewsItemId,GroupNewsItemName
                });
                LevelIndex = LevelIndex + 4;
                GetDataForTreeView(ref LevelIndex,ref dtTree, GroupNewsId, GroupNewsItemId);
                LevelIndex = LevelIndex - 4;
            }
        }

        [AjaxPro.AjaxMethod]
        public string BuildGroupNewsItem(string GroupNewsId,string ActiveGroupNewsItemId)
        {
            int LevelIndex = 0;
            DataTable dtTree=new DataTable();
            dtTree.Columns.Add("GroupNewsItemId");
            dtTree.Columns.Add("GroupNewsItemName");

            GetDataForTreeView(ref LevelIndex,ref dtTree,GroupNewsId,null);
            string SelectGroupNewsText = "<select id=drpSelectGroupNewsItem>\r\n";
            for (int iIndex = 0; iIndex < dtTree.Rows.Count; iIndex++)
            {
                string GroupNewsItemId = (string)dtTree.Rows[iIndex]["GroupNewsItemId"];
                string GroupNewsItemName = "(N/A)";
                try
                {
                    GroupNewsItemName = (string)dtTree.Rows[iIndex]["GroupNewsItemName"];
                }
                catch { }
                if (ActiveGroupNewsItemId.Equals(GroupNewsItemId))
                {
                    SelectGroupNewsText += "<option selected value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>\r\n";
                }
                else
                {
                    SelectGroupNewsText += "<option value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>\r\n";
                }
            }
            SelectGroupNewsText += "</select>\r\n";
            return SelectGroupNewsText;
        }

        public string RenderWebPart(ConfigParamCls ConfigParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(InputNewsConfig));
            string Html = "";

            string WebPartId = (string)Environments.Request("WebPartId");
            string RecordId = (string)Environments.Request("RecordId");

            string XmlConfigItem = Environments.GetXmlWebPartConfigItem(RecordId);
            string ActiveGroupNewsId = "";
            string ActiveGroupNewsItemId = "";
            if (XmlConfigItem != null)
            {
                try
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(new System.IO.StringReader(XmlConfigItem));
                    ActiveGroupNewsId = (string)ds.Tables[0].Rows[0]["GroupNewsId"];
                    ActiveGroupNewsItemId = (string)ds.Tables[0].Rows[0]["GroupNewsItemId"];
                }
                catch { }
            }

            string FirstGroupNewsItemId = "";
            DataTable dtGroupNews = new GroupNewsBLL().ReadingGroupNews();
            string SelectGroupNewsText = "<select id=drpSelectGroupNews onchange=\"javascript:ClientDetectGroupNewsItem();\">\r\n";
            for (int iIndex = 0; iIndex < dtGroupNews.Rows.Count; iIndex++)
            {
                string GroupNewsId = (string)dtGroupNews.Rows[iIndex]["GroupNewsId"];
                string GroupNewsName = (string)dtGroupNews.Rows[iIndex][Portal.Environment.Environments.ActiveLanguage+"Subject"];
                if (GroupNewsId.Equals(ActiveGroupNewsId))
                {
                    if (FirstGroupNewsItemId.Trim().Length == 0)
                    {
                        FirstGroupNewsItemId = GroupNewsId;
                    }
                    SelectGroupNewsText += "<option selected value=\"" + GroupNewsId + "\">" + GroupNewsName + "</option>\r\n";
                }
                else
                {
                    SelectGroupNewsText += "<option value=\"" + GroupNewsId + "\">" + GroupNewsName + "</option>\r\n";
                }
            }
            if (FirstGroupNewsItemId.Trim().Length == 0 && dtGroupNews.Rows.Count>0)
            {
                FirstGroupNewsItemId = (string)dtGroupNews.Rows[0]["GroupNewsId"];
            }

            SelectGroupNewsText += "</select>\r\n";
            Html +=
                  "<script language=javascript>\r\n" +
                  "     function ClientSaveConfig(WebPartId,RecordId)\r\n" +
                  "     {\r\n" +
                  "         GroupNewsIndex=document.getElementById(\"drpSelectGroupNews\").selectedIndex;\r\n" +
                  "         GroupNewsId=document.getElementById(\"drpSelectGroupNews\").options[GroupNewsIndex].value;\r\n" +

                  "         GroupNewsItemIndex=document.getElementById(\"drpSelectGroupNewsItem\").selectedIndex;\r\n" +
                  "         GroupNewsItemd=document.getElementById(\"drpSelectGroupNewsItem\").options[GroupNewsItemIndex].value;\r\n" +
                  "         rValue=Portal.CMS.InputNewsConfig.ServerSideSaveConfig(WebPartId,RecordId,GroupNewsId,GroupNewsItemd).value;\r\n" +
                  "         alert(rValue);\r\n" +
                  "     }\r\n" +
                  "     function ClientDetectGroupNewsItem()\r\n"+
                  "     {\r\n"+
                  "         GroupNewsIndex=document.getElementById(\"drpSelectGroupNews\").selectedIndex;\r\n" +
                  "         GroupNewsId=document.getElementById(\"drpSelectGroupNews\").options[GroupNewsIndex].value;\r\n" +
                  "         ActiveGroupNewsItemId=document.getElementById(\"txtActiveGroupNewsItemId\").value;\r\n"+
                  "         rValue=Portal.CMS.InputNewsConfig.BuildGroupNewsItem(GroupNewsId,ActiveGroupNewsItemId).value;\r\n"+
                  "         document.getElementById(\"divGroupNewsItem\").innerHTML=rValue;\r\n"+
                  "     }\r\n"+
                  "</script>\r\n" +
                  "<input type=hidden value=\""+ActiveGroupNewsItemId+"\" id=\"txtActiveGroupNewsItemId\">" +
                  "<table>" +
                  " <tr>" +
                  "     <td>" + ResourceBLL.GetText(Id, "GroupNews", "Nhóm") + ":</td>" +
                  "     <td>" + SelectGroupNewsText + "</td>" +
                  " </tr>" +
                  " <tr>" +
                  "     <td>" + ResourceBLL.GetText(Id, "GroupNewsItem", "Nhóm tin") + ":</td>" +
                  "     <td><div id=\"divGroupNewsItem\">"+BuildGroupNewsItem(FirstGroupNewsItemId,ActiveGroupNewsItemId)+"</td>" +
                  " </tr>" +
                 
                  " <tr>" +
                  "     <td></td>" +
                  "     <td><input onclick=\"javascript:ClientSaveConfig('" + WebPartId + "','" + RecordId + "');\" type=button value=\"" + ResourceBLL.GetText(Id, "Save", "Ghi lại") + "\"><input type=button value=\"" + ResourceBLL.GetText(Id, "Cancel", "Quay lại") + "\" onclick=javascript:window.close();></td>" +
                  " </tr>" +
                  "</table>";
            return Environments.GetFormDialog(Html, Subject);
        }

        public object Reserved { get { return null; } }
        public object SetReserved(object oInput)
        {
            return null;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveConfig(
            string WebPartId,
            string RecordId,
            string GroupNewsId,
            string GroupNewsItemId)
        {
            try
            {
                string XmlConfig = "";
                DataSet ds = new DataSet("XmlConfig");
                ds.Tables.Add("Config");
                ds.Tables["Config"].Columns.Add("WebPartId");
                ds.Tables["Config"].Columns.Add("RecordId");
                ds.Tables["Config"].Columns.Add("GroupNewsId");
                ds.Tables["Config"].Columns.Add("GroupNewsItemId");
                ds.Tables["Config"].Rows.Add(new object[]{
                    WebPartId,RecordId,GroupNewsId,GroupNewsItemId
                });
                XmlConfig = ds.GetXml();
                Environments.SaveWebPartItemConfig(WebPartId, RecordId, XmlConfig);
                return ResourceBLL.GetText(WebPartId, "SaveOk", "Ghi lại cấu hình thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public void AjaxProReg(System.Web.UI.Page page)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(InputNewsConfig), page);
        }
    }
}
